<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Candidate extends MY_Admin {

    protected $data;

    function __construct () {
        parent::__construct();

        $this->load->model(array('category_model','apply_model'));
        $this->load->library('MY_FormParam');
        
        $arrCategory = $this->category_model->getCategoryList();
        $this->data['category'] = MY_Helper::convArray($arrCategory, 'category_id', 'category_name');
    }

    function index(){
        $objFormParam = $this->my_formparam;
        $this->initSearchParam($objFormParam);
        
        $objFormParam->setParam($_POST);
        $objFormParam->trimParam();
        
        $this->data['arrForm']   = $objFormParam->getFormParamList();
        $this->data['arrHidden'] = $objFormParam->getSearchArray();
        
        $mode = $this->input->post('mode');
        switch ($mode){
//             case "delete":
//                 $apply_id = $this->input->post('apply_id');
//                 if(!$this->delete('apply',array('apply_id' => $apply_id))) {
//                     $this->data["msg"] = "削除が失敗しました。";
//                 } else {
//                     $this->data["msg"] = "削除が完了しました。";
//                 }
            case "search":
            default:
                $params = $objFormParam->getHashArray();
                $pager = array(
                        "cur_page"    => $this->input->post('search_pageno'),
                        "total_rows"  => count($this->apply_model->getApplyList($params)),
                );
                $this->setPager($pager);
                $per_page = $this->pager['per_page'];
                $offset   = $this->offset();

                $this->data['apply']     = $this->apply_model->getApplyList($params, true, $per_page, $offset);
                $this->data['apply_cnt'] = $this->pager['total_rows'];
                $this->data['pager'] 	 = $this->getPagerTpl();
                break;
        }
        $this->view('admin/apply_list', $this->data);
    }

    function edit() {
        $objFormParam = $this->my_formparam;
        $this->initSearchParam($objFormParam);
        $this->initFormParam($objFormParam);
        
        $objFormParam->setParam($_POST);
        $objFormParam->trimParam();
        
        $this->data['arrHidden'] = $objFormParam->getSearchArray();
        $this->data['arrForm']   = $objFormParam->getFormParamList();
        
        $mode = $this->input->post('mode');
        switch ($mode)
        {
            case "pre_edit":
                $apply_id = $this->input->post('apply_id');
                $arrData = $this->apply_model->getApplyById($apply_id);
                $objFormParam->setParam($arrData);

                $this->data['arrForm']  = $objFormParam->getFormParamList();
                $this->data["apply_id"] = $apply_id;

                $template = 'admin/apply_detail';
                break;
            default:
                $template = 'admin/apply_detail';
                break;
        }
        $this->view($template, $this->data);
    }
    
    public function initSearchParam(&$objFormParam)
    {
        $objFormParam->addParam('PageMax', 'search_page_max','',false);
        $objFormParam->addParam('PageNo', 'search_pageno','',false);
        $objFormParam->addParam('Display Search Form', 'search_form_display','',false);
    }
    
    public function initFormParam(&$objFormParam)
    {
        $objFormParam->addParam('人材ID'     , 'apply_id');
        $objFormParam->addParam('人材名'     , 'name');
        $objFormParam->addParam('国家'       , 'nation');
        $objFormParam->addParam('電話番号'   , 'phone');
        $objFormParam->addParam('メール'      , 'email');
        $objFormParam->addParam('仕事コード'   , 'job_code');
        $objFormParam->addParam('添付ファイル'     , 'attach');
        $objFormParam->addParam('添付ファイルパーツ' , 'save_attach');
    }
}
